package com.fz.us.admin.base.bean.search.rule;

import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.utils.CommonUtil;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - RuleNone
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-13 。
 * ============================================================================
 */
public class RuleNone extends Rule {

    private final String field;

    private RuleNone(SearchEnum.RuleType ruleType, String field) {
        super(ruleType);
        this.field = field;
    }

    public String getField() {
        return field;
    }

    public static RuleNone isEmpty(String field) {
        return newBuilder().setRuleType(SearchEnum.RuleType.isEmpty).setField(field).build();
    }

    public static RuleNone isNotEmpty(String field) {
        return newBuilder().setRuleType(SearchEnum.RuleType.isNotEmpty).setField(field).build();
    }

    public static RuleNone isNull(String field) {
        return newBuilder().setRuleType(SearchEnum.RuleType.isNull).setField(field).build();
    }

    public static RuleNone isNotNull(String field) {
        return newBuilder().setRuleType(SearchEnum.RuleType.isNotNull).setField(field).build();
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder extends Rule.Builder<Rule, Builder> {

        private String field;

        @Override
        protected Builder getThis() {
            return this;
        }

        public Builder setField(String field) {
            this.field = field;
            return this;
        }

        @Override
        public RuleNone build() {
            Assert.notNull(ruleType, "ruleType 不能为空");
            Assert.hasText(field, "field 不能为空");
            Assert.isTrue(CommonUtil.isField(field), "field 非法参数");
            return new RuleNone(ruleType, field);
        }

    }

}
